package ma.jee.portailReservation.service.person.Impl;

import ma.jee.portailReservation.persistence.person.Client;
import ma.jee.portailReservation.persistence.person.IClientDAO;
import ma.jee.portailReservation.persistence.reservation.IReglementDAO;
import ma.jee.portailReservation.service.person.DisplayClient;
import ma.jee.portailReservation.service.person.IDisplayClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: soufiane
 * Date: 27/12/11
 * Time: 00:00
 * To change this template use File | Settings | File Templates.
 */
@Service("defaultDisplayClientService")
public class DefaultDisplayClientService implements IDisplayClientService{

    @Autowired
	@Qualifier("hibernateClientDAO")
	private IClientDAO clientDao;

    @Autowired
	@Qualifier("hibernateReglementDAO")
	private IReglementDAO reglementDAO;

    @Override
    @Transactional(readOnly=true)
    public List<DisplayClient> getAllDisplayClient() {
         List<DisplayClient> result = new ArrayList< DisplayClient >();

        for(Client client : clientDao.findAll()) {
                  result.add(new DisplayClient(client));
        }

        return result;
    }

    @Override
    @Transactional(readOnly=true)
    public DisplayClient createClient() {
        return new DisplayClient(new Client());
    }

    @Override
    @Transactional(readOnly=true)
    public String addClient(DisplayClient displayClient) {
        if(this.clientDao.findById(displayClient.getIdClient()) == null) {
      Client client = new Client();

      client.setIdClient(displayClient.getIdClient());
      client.setLastName(displayClient.getLastName()); // static, because the form field has not been developed yet.
      client.setFirstName(displayClient.getFirstName());
      client.setAddress(displayClient.getAddress());
      client.setTelephone(displayClient.getTelephone());
      client.setEmail(displayClient.getEmail());
      client.setUserName(displayClient.getUserName());
      client.setPassword(displayClient.getPassword());


      this.clientDao.persist(client);
      return "ok";
    } else {
      return "ko";
    }
    }

    @Override
    @Transactional(readOnly=false)
    public String removeClient(DisplayClient displayClient) {
        Client client = this.clientDao.findByClientId(displayClient.getIdClient());

    if(client != null) {
      this.clientDao.remove(client);
      return "ok";
    }
    return "ko";
    }

    /*  @Override
 @Transactional(readOnly = false)
 public boolean createClient(final DisplayClient displayClient) {

     final Client client = new Client();

     if (!displayClient.getLastName().isEmpty()) {
         client.setLastName(displayClient.getLastName());
     }
     if (!displayClient.getFirstName().isEmpty()) {
         client.setFirstName(displayClient.getFirstName());
     }
     if (!displayClient.getTelephone().isEmpty()) {
         client.setTelephone(displayClient.getTelephone());
     }

     if (!displayClient.getAddress().isEmpty()) {
         client.setAddress(displayClient.getAddress());
     }

     if (!displayClient.getEmail().isEmpty()) {
        client.setEmail(displayClient.getEmail());
     }

     if (!displayClient.getPassword().isEmpty()) {
        client.setPassword(displayClient.getPassword());
     }

     if (!displayClient.getUserName().isEmpty()) {
        client.setUserName(displayClient.getUserName());
     }



     // Checks whether a unit already exists
    for (Client persistClient : clientDao.findAll()) {
         if (persistClient.getLastName().equals(client.getLastName())) {
             //context.addMessage(new MessageBuilder().error().arg(client.getLastName()).code("client.name.unique").build());
             return false;
         }
     }


     // persists the unit into the db and displays the valuable message
     // context
     clientDao.persist(client);

     clientDao.flush();
     //context.addMessage(new MessageBuilder().info().arg(client.getLastName()).code("client.persist").build());

     return true;
 }      */


}
